package com.itheima.edy.info.manager.controller;

import com.itheima.edy.info.manager.domain.Student;
import com.itheima.edy.info.manager.server.StudentServer;

import java.util.Scanner;

public class BaseStudentController<delId> {
    StudentServer studentServer = new StudentServer();
    private Scanner sc = new Scanner(System.in);

    public void start() {

        StudentM:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    //System.out.println("添加学生");
                    addStudent();
                    break;
                case "2":
                    //System.out.println("删除学生");
                    deleteStudentById();
                    break;
                case "3":
                    //System.out.println("修改学生");
                    undateStudent();
                    break;
                case "4":
                    //System.out.println("查看学生");
                    findAllStudent();
                    break;
                case "5":
                    System.out.println("感谢使用学生管理系统。");
                    break StudentM;
                default:
                    System.out.println("您输入的有误，请重新输入");
                    break;
            }
        }
    }

    public void undateStudent() {
        String updateId = inputStudentId();
        Student newStu = inputStudentInfo(updateId);

        studentServer.updateStudent(updateId, newStu);
        System.out.println("修改成功");
    }

    public void deleteStudentById() {
        String delId = inputStudentId();
        //调用业务员中的方法根据id删除学生
        studentServer.deleteStudentById(delId);
        //提示删除chengg
        System.out.println("删除成功");
    }

    public void findAllStudent() {
        //调用业务员中的获取方法，得到学生的对下个数组
        Student[] students = StudentServer.findAllStudent();
        //判断数组的内存地址，是否为null
        if (students == null) {
            System.out.println("查无信息，请添加后重试");
            return;
        }
        //遍历数组，获取学生信息并打印在控制台
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < students.length; i++) {
            Student student = students[i];
            if (student != null) {
                System.out.println(student.getId() + "\t" + student.getName() + "\t" + student.getAge() + "\t\t" + student.getBirthday());
            }
        }
    }

    public void addStudent() {
        //从键盘录入学生信息
        String id;
        while (true) {
            System.out.println("请输入学生id");
            id = sc.next();
            //判断输入的学号是否存在
            boolean flag = studentServer.isExists(id);
            if (flag) {
                //如果存在
                System.out.println("学号已被占用，请重新输入");
            } else {
                //不存在就结束循环然后开始输入信息
                break;
            }
        }
        Student stu = inputStudentInfo(id);
        //将学生对象传递到studentServer中的addStudent
        boolean result = studentServer.addStudent(stu);
        //根据返回的boolean类型结果，在控制台打印true/false

        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    public String inputStudentId() {
        String id;
        while (true) {
            //键盘录入要删除的学生id
            System.out.println("请输入学生的id");
            id = sc.next();
            //判断id在容器中是否存在，如果不存在，则需要一直录入
            boolean exists = studentServer.isExists(id);
            if (!exists) {
                System.out.println("您输入的id不存在，请重新输入");
            } else {
                break;
            }
        }
        return id;
    }

    public Student inputStudentInfo(String id){
        return null;
    }
}
